package com.wondream.myframework.juc;

import org.junit.Test;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

/**
 * @author wondream
 * @version 1.0
 * @create 2022/5/22 11:41 AM
 */
public class JUC02AtomicIntegerFieldUpdaterTest {

    AtomicIntegerFieldUpdater<JUC02AtomicIntegerData> getUpdater(String fieldName){
        return AtomicIntegerFieldUpdater.newUpdater(JUC02AtomicIntegerData.class, fieldName);
    }

    void doit(){
        JUC02AtomicIntegerData juc02AtomicIntegerData = new JUC02AtomicIntegerData();
        System.out.println("1 ==> "+getUpdater("value1").getAndSet(juc02AtomicIntegerData, 10));
        System.out.println("3 ==> "+getUpdater("value2").incrementAndGet(juc02AtomicIntegerData));
        System.out.println("2 ==> "+getUpdater("value3").decrementAndGet(juc02AtomicIntegerData));
        // can not access a member of class JUC02AtomicIntegerData with modifiers "private volatile"
        System.out.println("true ==> "+getUpdater("value4").compareAndSet(juc02AtomicIntegerData, 4, 5));
    }

    @Test
    public void run(){
        JUC02AtomicIntegerFieldUpdaterTest juc02AtomicIntegerFieldUpdater = new JUC02AtomicIntegerFieldUpdaterTest();
        juc02AtomicIntegerFieldUpdater.doit();
    }
}
